package com.example.backend.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.entity.*;
import com.example.backend.mapper.*;
import com.example.backend.service.SharedBookletService;
import com.example.backend.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 共享单词本相关服务实现
 * @author 李月
 * @date 2022/5/4 16:56
 */
@Component
public class SharedBookletServiceImpl implements SharedBookletService {
    @Autowired
    private SharedBookletMapper sharedBookletMapper;
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private BookletMapper bookletMapper;
    @Autowired
    private BookletEntryMapper bookletEntryMapper;
    @Autowired
    private EntryMapper entryMapper;
    @Autowired
    private UserBookletMapper userBookletMapper;
    @Autowired
    private CollectionBookletMapper collectionBookletMapper;

    @Override
    public int shareBooklet(int bookletId) {
        SharedBooklet sharedBooklet = new SharedBooklet();
        QueryWrapper<SharedBooklet> wrapper = new QueryWrapper<>();
        wrapper.eq("booklet_id", bookletId);
        if(sharedBookletMapper.selectOne(wrapper) != null)  //已分享
            return 1;
        sharedBooklet.setBooklet_id(bookletId);
        sharedBookletMapper.insert(sharedBooklet);
        if(sharedBookletMapper.selectOne(wrapper) != null)  //分享成功
            return 0;
        return 2;
    }

    @Override
    public PageUtils getSharedBooklets(int page, int pageSize) {
        Page<BookletVO> p = new Page<>(page, pageSize);
        p.setRecords(this.sharedBookletMapper.getBooklet(p));
        return new PageUtils(p);
    }

    @Override
    public int collection(int bookletId, int userId) {
        QueryWrapper<SharedBooklet> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("booklet_id", bookletId);
//        QueryWrapper<Collection> wrapper2 = new QueryWrapper<>();
//        wrapper2.eq("user_id", userId).eq("shared_id", sharedId);
        CollectionVO collectionVO = collectionMapper.getCollection(bookletId, userId);
        if(collectionVO != null){   //已收藏
            return 1;
        }
        //转换为个人单词本
        if(sharedBookletMapper.getSharedBooklet(bookletId) != null) {
            int sharedId = sharedBookletMapper.getSharedBooklet(bookletId).getShared_id();
            Booklet booklet = bookletMapper.selectById(bookletId);
            Booklet collectBooklet = new Booklet();
            collectBooklet.setBooklet_name(booklet.getBooklet_name());
            collectBooklet.setBooklet_intro(booklet.getBooklet_intro());
            Date date = new Date();
            Timestamp time = new Timestamp(date.getTime());
            collectBooklet.setCreate_date(time);
            bookletMapper.insert(collectBooklet);

            int collectBookletId = collectBooklet.getBooklet_id();
            List<Integer> entryList = bookletEntryMapper.getEntryId(bookletId);
            for (int entryId : entryList) {
                Entry entry = entryMapper.selectById(entryId);
                Entry collectEntry = new Entry();
                collectEntry.setEntry_word_id(entry.getEntry_word_id());
                collectEntry.setRemark(entry.getRemark());
                collectEntry.setStudy_count(0);
                collectEntry.setLast_study_datetime(java.sql.Date.valueOf("2022-01-01"));
                collectEntry.setDeleted(0);
                entryMapper.insert(collectEntry);
                int collectEntryId = collectEntry.getEntry_id();
                BookletEntry bookletEntry = new BookletEntry();
                bookletEntry.setEntry_id(collectEntryId);
                bookletEntry.setBooklet_id(collectBookletId);
                bookletEntryMapper.insert(bookletEntry);
            }
            UserBooklet userBooklet = new UserBooklet();
            userBooklet.setUser_id(userId);
            userBooklet.setBooklet_id(collectBookletId);
            userBookletMapper.insert(userBooklet);
            Collection collection = new Collection();
            collection.setUser_id(userId);
            collection.setShared_id(sharedId);
            collectionMapper.insert(collection);
            CollectionBooklet collectionBooklet = new CollectionBooklet();
            collectionBooklet.setOld_booklet_id(bookletId);
            collectionBooklet.setNew_booklet_id(collectBookletId);
            collectionBookletMapper.insert(collectionBooklet);
            QueryWrapper<Collection> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId).eq("shared_id", sharedId);
            if (collectionMapper.selectOne(wrapper) != null) {
                return 0;
            }
        }
        return 2;
    }

    @Override
    public Map<String, Object> getSharedBookletSpecification(int bookletId) {
        Map<String, Object> map = new HashMap<>();
        Booklet booklet = bookletMapper.selectById(bookletId);
        map.put("bookletName", booklet.getBooklet_name());
        map.put("bookletAuthor", userBookletMapper.getBookletAuthor(bookletId));
        map.put("bookletIntro", booklet.getBooklet_intro());
        map.put("bookletEntryCount", bookletEntryMapper.getEntryCount(bookletId));
        Date date = new Date(booklet.getCreate_date().getTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("bookletCreateDate", simpleDateFormat.format(date));
        return map;
    }

    @Override
    public int deleteSharedBooklet(int bookletId) {
        QueryWrapper<SharedBooklet> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("booklet_id", bookletId);
        SharedBooklet sharedBooklet = sharedBookletMapper.selectOne(wrapper1);
        if(sharedBooklet != null) {
            int sharedId = sharedBooklet.getShared_id();
            QueryWrapper<Collection> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("shared_id", sharedId);
            sharedBookletMapper.deleteById(sharedBooklet);
            if (collectionMapper.selectList(wrapper2) != null) {
                collectionMapper.delete(wrapper2);
            }
            return 0;
        }
        return 2;
    }

}
